package net.caidingke.shepherd.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import io.ebean.SqlRow;
import net.caidingke.shepherd.LocationService;
import net.caidingke.shepherd.controller.base.BasicController;
import net.caidingke.shepherd.domain.common.City;
import net.caidingke.shepherd.exception.NotFoundException;
import net.caidingke.shepherd.utils.constant.GlobalConfig;
import net.caidingke.shepherd.utils.result.Result;
import net.caidingke.shepherd.utils.result.ResultPage;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * @author bowen.
 */
@RestController
@RequestMapping(value = GlobalConfig.API_PRE_FIX)
public class CityController extends BasicController {

    @Reference(version = "1.0.0")
    private LocationService locationService;

    @GetMapping("/cities")
    public ResultPage cities(int page, int pageSize) {
        return locationService.cities(page, pageSize);
    }

    @PostMapping("/city")
    public Result save(@RequestParam String name) {
        locationService.createCity(name);
        return ok();
    }

    @GetMapping("/city/{id}")
    public Result<City> byId(@PathVariable Long id) throws IOException {
        return ok(locationService.findCityById(id).orElseThrow(NotFoundException::new));
    }

    @PutMapping("/city/{id}")
    public Result<City> modify(@PathVariable Long id, @RequestParam String name) throws IOException {
        return ok(locationService.modifyCity(id, name).orElseThrow(NotFoundException::new));
    }

    @GetMapping("/native")
    public void selectNative() {
        List<SqlRow> list = locationService.selectNative();
        System.out.println(list.get(0));
    }

}
